# coding=utf-8
"""

"""

import threading
import redis

from utils.configs.db_conf import db_redis
from utils.do_log import c_log


# @Time    :  2024-11-28 16:58:40
# @Author  :  jiangtong
# @Email   :  gingerqgyy@outlook.com
# @Project :  test_curl_convert
# @File    :  do_redis

class DoRedis:
    _instance_lock = threading.Lock()
    _redis_client = None
    _instance = None

    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, '_instance') or not cls._instance:
            with cls._instance_lock:
                if not hasattr(cls, '_instance') or not cls._instance:
                    cls._instance = super().__new__(cls)
        return cls._instance

    def __init__(self):
        if self._redis_client is None:
            self._redis_client = redis.StrictRedis(**db_redis, decode_responses=True)

    def get(self, key):
        """
        根据 key 获取 value
        当 redis 中不存在该 key 时, 返回 None
        :param key:
        :return: value
        """
        _value = self._redis_client.get(key)
        c_log.debug(f'Redis 取值, 根据 << {key} >> 取到值 << {_value} >>')
        return _value


redis_cli = DoRedis()

# 使用示例 :
if __name__ == '__main__':
    value: str = redis_cli.get('AUTH:VERIFY_CODE:ed9b7fb8-d646-4464-bb07-1e14a2a81d31')

    print(value.strip('"') if value else 'key 不存在')
    print(type(value))

    # 单例调试 :
    """
    def _singleton():
        redis_client = DoRedis()
        val = redis_client.get('AUTH:VERIFY_CODE:2ded2751-0646-4a9e-b206-78153ad09c3f')
        print(f"Thread ID: {threading.get_ident()}, Redis Client ID: {id(redis_client)}, Value: {val}")


    threads = []
    for i in range(10):
        thread = threading.Thread(target=_singleton)
        threads.append(thread)
        thread.start()

    for thread in threads:
        thread.join()
    """
